package com.binvshe.binvshe.opus;

import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.binvshe.binvshe.R;
import com.binvshe.binvshe.common.http.DataRequestListener;
import com.binvshe.binvshe.common.http.MyHttpRequest;
import com.binvshe.binvshe.common.http.UrlCenter;
import com.binvshe.binvshe.entity.StoryContentEntity;
import com.f2prateek.dart.Dart;
import com.f2prateek.dart.InjectExtra;
import com.f2prateek.dart.Nullable;
import com.pachong.android.baseuicomponent.activity.BaseActivity;
import com.pachong.android.framework.httprequest.IHttpRequest;
import com.pachong.android.framework.httprequest.UrlParams;
import com.pachong.android.frameworkbase.utils.CharSpanUtil;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static android.R.attr.data;
import static com.binvshe.binvshe.R.id.mTvContent;
import static com.binvshe.binvshe.R.id.tvMenu;

/**
 * Created by Zoi.
 * E-mail：KyluZoi@gmail.com
 * 2017/2/7
 */

public class StoryDetailsActivity extends BaseActivity {
    private static final String KEY_ARTICLE_ID = "ARTICLE_ID";
    private static final String KEY_ID = "id";
    private static final String KEY_TITLE = "TITLE";


    @Bind(R.id.tvContent3)
    TextView mTvContent3;
    @Bind(R.id.tvMenuNum)
    TextView mTvMenuNum;



//    @Nullable
//    @InjectExtra(KEY_ID)
//    int mID = -1;
    @InjectExtra(KEY_ARTICLE_ID)
    int mAriticleID;
    @InjectExtra(KEY_TITLE)
    String mTitle;

    int mCurrent;
    int mTotal;
    private int mID;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent starter = getIntent();
        mID = starter.getIntExtra(KEY_ID, -1);
        Log.e("上一页传下来的id",""+ mID);

        ButterKnife.bind(this);
        Dart.inject(this);
        getStoryDetails(true, false);
        enableBackButton();
        getCustomToolbar().setTitle(mTitle);
    }

    public static void start(Context context, int articleID, int id, String title) {
        Intent starter = new Intent(context, StoryDetailsActivity.class);
        starter.putExtra(KEY_ID, id);
        Log.e("传送的id",id+"");
        starter.putExtra(KEY_ARTICLE_ID, articleID);
        starter.putExtra(KEY_TITLE, title);
        context.startActivity(starter);
    }

    public static void start(Context context, int articleID, String title) {
        Intent starter = new Intent(context, StoryDetailsActivity.class);
        starter.putExtra(KEY_ARTICLE_ID, articleID);
        starter.putExtra(KEY_TITLE, title);
        context.startActivity(starter);
    }

    @Override
    public View createDataView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.act_story_details, container, false);
    }

    @OnClick({R.id.btnPrevious, R.id.btnNext,R.id.tvMenu})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btnPrevious:
                if (mCurrent == 1) {
                    Toast.makeText(this, "当前已为第一页", Toast.LENGTH_SHORT).show();
                    return;
                }
                getStoryDetails(false, true);
                break;

            case R.id.btnNext:
                if(mCurrent ==mTotal){
                    Toast.makeText(this, "当前已为最后一页", Toast.LENGTH_SHORT).show();
                    return;
                }
                Log.e("点击了","点击了");
                getStoryDetails(true, true);
                break;

            case R.id.tvMenu:




                if(mID!=-1){
                    LongStoryMenuActivity.start(this, mAriticleID, mTitle);
                    Log.e("进来了不是-1","所得税的费");
                    return;
                }else{
                    Log.e("mID",mID+"");
                    finish();
                    Log.e("进来了-1","所得税的费");
                }


                break;
        }
    }

    /**
     * 获得长篇详情
     */
    private void getStoryDetails(boolean isNext, boolean needChange) {
        IHttpRequest request = new MyHttpRequest(this);
        String url = String.format(UrlCenter.GET_STORY_DETAILS, mAriticleID);
        UrlParams urlParams = new UrlParams();
        if (needChange) {
            if (isNext) {//下一页
                Log.e("isNext","isNext");
                urlParams.put("offset", "next");
            } else {//上一页
                urlParams.put("offset", "pre");
            }
        }
        if (mID != -1) {
            urlParams.put("id", mID + "");
        }
        request.get(url, urlParams, new DataRequestListener<StoryContentEntity>(StoryContentEntity.class) {
            @Override
            public void onStart(Context context, String url) {
                super.onStart(context, url);
            }

            @Override
            public void onSuccess(StoryContentEntity data) {
                super.onSuccess(data);
                setStoryData(data);
            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                super.onFailure(t, errorNo, strMsg);
            }
        });

    }

    private void setStoryData(StoryContentEntity data) {
        int titleLength = data.getTitle().length();
        //重新设置标题
        getCustomToolbar().setTitle(data.getTitle());
        mTvContent3.setText("");
        mTvContent3.append(
                CharSpanUtil.setTextSize(
                        CharSpanUtil.setTextStyle(data.getTitle(), 0, titleLength, Typeface.BOLD), 0, titleLength, 18, true));
        mTvContent3.append("\n\n" + data.getContent());
        Log.e("内容",data.getContent());
        mID = data.getId();
        mTvMenuNum.setText(data.getCurrent() + "/" + data.getTotal());

        mCurrent = data.getCurrent();
        mTotal = data.getTotal();
    }
}
